﻿namespace sqliteDemo.Common
{
    public class AppsettingHelper
    {
        //单例实现
        public static IConfiguration? _config;

        public AppsettingHelper(IConfiguration configuration)
        {
            _config = configuration;
        }

        /// <summary>
        /// 读取appsetting 实体
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="item"></param>
        /// <returns></returns>
        public static List<T> ReadAppSettings<T>(params string[] item)
        {

            List<T> instance = new List<T>();
            _config.Bind(string.Join(":", item), instance);
            return instance;
        }


        /// <summary>
        /// 读取指定节点的字符串
        /// params 可以输入多个string类型的项组成item:
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        public static string ReadAppSettings(params string[] item)
        {
            try
            {
                if (item.Any())
                {
                    return _config[string.Join(":", item)];
                }
            }
            catch
            {
                return "";
            }
            return "";
        }
    }
}
